;
(function (window, angular) {
    'use strict';
    angular.module("Controller.BasicData.Order", [])
        .controller("OrderCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.activeFlag = [true, false, false];
            $scope.markInfo = {
                curRow: 0,
                allChecked: false
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.dataList = [];
            $scope.condition = {};
            
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/materialTransfer/exportAll")
            };

            $scope.tableConfig = {
                fixedWidth: 0,
                scrollWidth: 900,
                tableCol: [
                    {
                        name: "订单号码",
                        dataField: "orderNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewDetail",
                    },
                    {
                        name: "到货工厂",
                        dataField: "factory",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "供货商代码",
                        dataField: "supplierNo",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "供货商名称",
                        dataField: "supplierName",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "订购日期",
                        dataField: "orderDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "到货日期",
                        dataField: "arriveDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                    },
                ]
            };
            $scope.detailTbConf = {
                scrollWidth: 700,
                tableCol: [
                    {
                        name: "零件编号",
                        dataField: "componentNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "零件简号",
                        dataField: "materialsShortNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "是否批控制件",
                        dataField: "ispijianFlgShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "包装数",
                        dataField: "packageNum",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "包装数单位",
                        dataField: "packageUnit",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "箱数",
                        dataField: "xiangNum",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "订购数量",
                        dataField: "orderNum",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "取消数量",
                        dataField: "cancelNum",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "验收状态",
                        dataField: "receiveStatusShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                ]
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false];
                $scope.activeFlag[index] = true;
            };

            var btnPermissionCode = {};

            $scope.btnPermission = {};

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {left: $scope.tableConfig.fixedWidth + "px"}
                };
            };

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/gpsMmSwOrder/page";
                
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                var param = {
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                    filters: $scope.condition
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.orderDateShow = $filter("YbDate")(item.orderDate, "yyyy-MM-dd HH:mm:ss");
                        item.arriveDateShow = $filter("YbDate")(item.arriveDate, "yyyy-MM-dd HH:mm:ss");
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    $scope.markInfo.allChecked = checkAll();
                }
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.addInfo.materialList.length; i < len; i++) {
                    if (!$scope.addInfo.materialList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectAll = function () {
                angular.forEach($scope.addInfo.materialList, function (item) {
                    item.isChecked = $scope.markInfo.allChecked;
                });
            };
            function viewDetail(item) {
                var service = "device";
                var reqUrl = "/gpsMmSwOrder/item";
                var param = {
                    id: item.orderId
                };
                ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        return;
                    }
                    resp.value.orderDetailList?.forEach((item) => {
                        item.ispijianFlgShow = $filter('TrueOrFalse')(item.ispijianFlg);
                        item.receiveStatusShow = $filter('ProgressStatus')(item.receiveStatus, 'isReceive2');
                    })
                    $scope.orderObj = resp.value;
                    $scope.changeTab(1);
                });
            }

            $scope.cancel = function () {
                $scope.changeTab(0);
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);
